<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Student extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model("student_model");
			
			$username = $_SESSION['username'];
			$result = $this->student_model->getStudData($username);
			
			foreach($result as $item){
				$data[] = $item;
			}
			/* add student number to sessions. */
			$_SESSION['stdno'] = $data[1];
			
			$data['title'] = 'Student';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/student_homepage',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
    }
	
	public function loadBasicInfo(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model('student_model');
			
			$stdno = $_SESSION['stdno'];
			$result = $this->student_model->getStudData2($stdno);
			
			foreach($result as $item){
				$data[] = $item;
			}
			
			if($data[5] != NULL){
				$bday = $data[5];
				$token = strtok($bday, "/");

				$i=0;
				while ($token != false){
					$result['a' . $i] = $token;
					$token = strtok("/");
					$i = $i + 1;
				}
			
				if($result['a0'] == 01) $result['a0'] = 'January';
				if($result['a0'] == 02) $result['a0'] = 'February';
				if($result['a0'] == 03) $result['a0'] = 'March';
				if($result['a0'] == 04) $result['a0'] = 'April';
				if($result['a0'] == 05) $result['a0'] = 'May';
				if($result['a0'] == 06) $result['a0'] = 'June';
				if($result['a0'] == 07) $result['a0'] = 'July';
				if($result['a0'] == 08) $result['a0'] = 'August';
				if($result['a0'] == 09) $result['a0'] = 'September';
				if($result['a0'] == 10) $result['a0'] = 'October';
				if($result['a0'] == 11) $result['a0'] = 'November';
				if($result['a0'] == 12) $result['a0'] = 'December';
			}else{
				$result['a0'] = 'January';
				$result['a1'] = '01';
				$result['a2'] = '2012';
			}

			$result[$stdno] = $stdno;
		
			$data['title'] = "Student's Basic Information";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/student_modifybasicinfo',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function logout(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$_SESSION['login'] = 0;
			session_destroy();
			redirect('login');
		}else{
			redirect('login');
		}
	}	
	
	public function loadChangePassword(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$stdno = $_SESSION['stdno'];
			
			$data['title'] = "Change Password";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/student_changepassword');
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function changePassword(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $_SESSION['stdno'];
			
			$pw1 = $this->input->post('password');
			$pw2 = $this->input->post('cpassword');
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[6]|max_length[20]|matches[cpassword]');
			$this->form_validation->set_rules('cpassword', 'password confirmation', 'trim|required|min_length[6]|max_length[20]');
		
			if($this->form_validation->run() == FALSE){
				$data['title'] = "Change Password";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/student_changepassword');
				$this->load->view('templates/footer');
			}else{
				$data = array(
							'pw' => md5($pw1),
							'stdno' => $stdno,
						);
				$this->load->model('student_model');
				$this->student_model->changePW($data);
				
				$uname = $_SESSION['username'];
				$message = $uname . " changed his/her password.";
				$this->student_model->addToLog($message,$uname);
				
				redirect('/student/view/');
			}
		}else{
			redirect('login');
		}
	}
	
	public function modify_student_basicinfo(){
		if(($this->is_logged_in())){
			$this->load->library('form_validation');
			$this->load->helper(array('url','form'));
			
			$stdno = $_SESSION['stdno'];
			$p_address = $this->input->post('prov_address');
			$c_address = $this->input->post('col_address');
			$cpno = $this->input->post('mobile_no');
			$month = $this->input->post('month');
			$day = $this->input->post('day');
			$year = $this->input->post('year');
			$bday = $month . '/' . $day . '/' . $year;
			$email = $this->input->post('email');
			
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
			$this->form_validation->set_rules('prov_address','Provincial Address','trim|min_length[5]|max_length[50]');
			$this->form_validation->set_rules('col_address','College Address','trim|min_length[5]|max_length[50]');
			$this->form_validation->set_rules('mobile_no','Mobile Number','trim|integer|min_length[11]|max_length[11]');
			
			
			if($this->form_validation->run() == FALSE){
				$this->load->model('student_model');
				$result = $this->student_model->getStudData2($stdno);
			
				foreach($result as $item){
					$data[] = $item;
				}
				
				$bday = $data[5];
				$token = strtok($bday, "/");

				$i=0;
				while ($token != false){
					$result['a' . $i] = $token;
					$token = strtok("/");
					$i = $i + 1;
				}
				
				if($result['a0'] == 01) $result['a0'] = 'January';
				if($result['a0'] == 02) $result['a0'] = 'February';
				if($result['a0'] == 03) $result['a0'] = 'March';
				if($result['a0'] == 04) $result['a0'] = 'April';
				if($result['a0'] == 05) $result['a0'] = 'May';
				if($result['a0'] == 06) $result['a0'] = 'June';
				if($result['a0'] == 07) $result['a0'] = 'July';
				if($result['a0'] == 08) $result['a0'] = 'August';
				if($result['a0'] == 09) $result['a0'] = 'September';
				if($result['a0'] == 10) $result['a0'] = 'October';
				if($result['a0'] == 11) $result['a0'] = 'November';
				if($result['a0'] == 12) $result['a0'] = 'December';
		  
				$result[$stdno] = $stdno;
			
				$data['title'] = "Student's Basic Information";
				$this->load->view('templates/header', $data);
				$this->load->view('pages/student_modifybasicinfo',$result);
				$this->load->view('templates/footer');
			}else{
				$data = array(
					'stdno' => $stdno,
					'p_address' => $p_address,
					'c_address' => $c_address,
					'cpno' => $cpno,
					'bday' => $bday,
					'email' => $email,
					);
				$this->load->model('student_model');
				$this->student_model->modifybasicinfo($data);
				
				$uname = $_SESSION['username'];
				$message = $uname . " modified his/her Basic Information.";
				$this->student_model->addToLog($message,$uname);
				
				redirect('/student/view/');
			}
		}else{
			redirect('login');
		}
	}
	
	public function view(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model("student_model");
			
			$username = $_SESSION['username'];
			$result = $this->student_model->getStudData($username);
			
			foreach($result as $item){
				$data[] = $item;
			}
			
			$data['title'] = 'Student';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/student_homepage',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
    public function loadAcademicInfo(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$stdno = $_SESSION['stdno'];
			
			$this->load->model("student_model");
			$result = $this->student_model->studAcadInfo($stdno);
			
			$data['title'] = "Student's Academic Information";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/student_viewBasicAcadInfo',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}
	
	public function loadListElectives(){
		if(($this->is_logged_in())){
			$this->load->helper(array('url','form'));
			$this->load->model('encoder_model');
			$result['e_cs'] = $this->encoder_model->getElectiveCS();
			$result['e_am'] = $this->encoder_model->getElectiveAM();
			$result['e_dc'] = $this->encoder_model->getElectiveDC();
			$result['e_em'] = $this->encoder_model->getElectiveEM();
			$result['e_hum'] = $this->encoder_model->getElectiveHUM();
			$result['e_frm'] = $this->encoder_model->getElectiveFRM();
			$result['e_p'] = $this->encoder_model->getElectiveP();
			$result['e_ss'] = $this->encoder_model->getElectiveSS();
			$result['e_stat'] = $this->encoder_model->getElectiveSTAT();
			$result['e_ie'] = $this->encoder_model->getElectiveIE();
			
			$data['title'] = "List of Electives Allowed to be Taken";
			$this->load->view('templates/header', $data);
			$this->load->view('pages/student_electivesallowedtotake',$result);
			$this->load->view('templates/footer');
		}else{
			redirect('login');
		}
	}

	public function sendMessage() {
		if($this->is_logged_in()) {
			$this->load->model('student_model');
			$data['received'] = $this->student_model->getMessages($_SESSION['username']);
			$data['title'] = 'Send Message';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/student_message', $data);
			$this->load->view('templates/footer');
		} else {
			redirect('login');
		}
	}

	public function messageSend() {
		if($this->is_logged_in()) {
			$detail = $this->input->post('detail');

			$this->load->model('student_model');
			$this->student_model->sendMessage($_SESSION['username'], $detail);
		} else {
			redirect('login');
		}
	}
}

/* End of file  */
/* Location: ./application/controllers/ */